//
//  NetWorkTool.swift
//  FruitJuice
//
//  Created by Rae on 2023/12/20.
//

import UIKit

let RT = RequestNetWorkTool.shared

class RequestNetWorkTool: NSObject {
    
    static let shared = RequestNetWorkTool()
    
    //MARK: 上传照片
    func uploadCoverImage(file:Data, Handler comp:@escaping ((InfoUtil.uplpadImg) -> Void)) {
        var para:[String:Any] = [:]
        para["file"] = file
        
        RequestManger.uploadImgRequest(UrlString: uploadImageUrl, params: para) { (response) in
            
            let imgDict = response as NSDictionary
            comp(InfoUtil.uplpadImg.deserialize(from: imgDict)!)
            
        } failure: { (error) in
            print(error)
        }
    }
    
    //MARK: 注册
    func registerWith(emailNum num:String, Password pwd:String, Handler comp:@escaping ((String) -> Void)) {
        var para:[String : Any] = [:]
        para["email"] = num
        para["pwd"] = pwd
        para["info9"] = "FruitJuice___"
        RequestManger.postRequest(UrlString: regUrl, params: para, success: { (response) in
            
            let jsonData = InfoUtil.register.deserialize(from: response)
            USEINFOMODEL.id = jsonData?.userId ?? ""
            comp(jsonData?.userId ?? "")
            
        }) { (error) in
            print(error)
        }
    }
    
    //MARK: 登录
    func appleLogininWith(emailNum email:String, Password pwd:String, Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        var para:[String : Any] = [:]
        para["pwd"] = pwd
        para["identifier"] = email
        RequestManger.postRequest(UrlString: appleLoginUrl, params: para, success: { (response) in
            
            print("getUserInfoUrlInfoWith=-----   ")
            print(response)

            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            USEINFOMODEL = jsonData!
            let userdefalut = UserDefaults.standard
            userdefalut.setValue(jsonData?.toJSON(), forKey: "UserInfo")
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }
    
    //MARK: 编辑用户信息
    func initUserInfoWith(paraInfo paraInfoDict:[String:Any], Handler comp:@escaping ((InfoUtil.initInfo) -> Void)) {
        
        var par = [String:Any]()
        par = paraInfoDict
        par["info9"] = "FruitJuice___"
        RequestManger.postRequest(UrlString: reg_infoUrl, params: par, success: { (response) in
            let jsonData = InfoUtil.initInfo.deserialize(from: response)
            print("getUserInfoUrlInfoWith=-----   ")
            print(response)
            USEINFOMODEL = jsonData!
            let userdefalut = UserDefaults.standard
            userdefalut.setValue(jsonData?.toJSON(), forKey: "UserInfo")
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }
    
    //MARK: 创建房间
    func createHouse(paraInfo paraInfoDict:[String:Any], Handler comp:@escaping ((InfoUtil.room) -> Void)) {
        RequestManger.postRequest(UrlString: createHouseurl, params: paraInfoDict, success: { (response) in
            let jsonData = InfoUtil.room.deserialize(from: response)
            print("creteHouse===-----   ")
            print(jsonData)
            comp(jsonData!)
        }) { (error) in
            print(error)
        }
    }
    
    //MARK: 房间列表
    func getRoomList(Handler comp:@escaping (([InfoUtil.room?]) -> Void)) {
        
        var para:[String : Any] = [:]
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: houseList, params: para, success: { (response) in
            print("GetRoomListWith=-----   ")
            print(response)
            let jsonData = [InfoUtil.room].deserialize(from: response)
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }
    
    //MARK: 发表评论
    func sentCommit(houseId:String,msg:String,userId:String,Handler comp:@escaping ((String) -> Void)) {
        
        var para:[String : Any] = [:]
        para["houseId"] = houseId
        para["msg"] = msg
        para["userId"] = userId
        RequestManger.postRequest(UrlString: addCommit, params: para, success: { (response) in
            print(response)
            comp("1")
        }) { (error) in
            print(error)
        }
    }
    
    //MARK: 评论列表
    func getCommentList(houseId:String,Handler comp:@escaping (([InfoUtil.commentDta?]) -> Void)) {
        
        var para:[String : Any] = [:]
        para["houseId"] = houseId
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: houseCommits, params: para, success: { (response) in
            print("GetCommentListWith=-----   ")
            print(response)
            let jsonData = [InfoUtil.commentDta].deserialize(from: response)
            comp(jsonData!)
            
        }) { (error) in
            print(error)
        }
    }
    
    //MARK: 拉黑 类型 0取消拉黑 1拉黑
    func blockWithUserid(uid:String,type:String,Handler comp:@escaping ((String) -> Void)) {
        var para:[String : Any] = [:]
        para["otherUserId"] = uid
        para["type"] = type
        para["userId"] = USEINFOMODEL.id
        RequestManger.postRequest(UrlString: blockAnchor, params: para, success: { (response) in
            comp("1")
        }) { (error) in
            print(error)
        }
    }
}
